package com.bjx.system.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjx.common.security.domain.UserPostVO;
import com.bjx.api.system.domain.vo.UserRoleVO;
import com.bjx.api.system.domain.vo.UserVO;
import com.bjx.common.core.domain.R;
import com.bjx.common.core.domain.group.Update;
import com.bjx.api.system.domain.query.UserQuery;
import com.bjx.system.domain.vo.UpdatePasswordVO;
import com.bjx.system.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: 用户控制器
 * @author: liushuai
 * @date: 2022/6/30 19:16
 */
@Tag(name = "用户控制器")
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private IUserService userService;


    /**
     * 分页查询组织机构
     * @param userQuery
     * @return: com.bjx.common.core.domain.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.bjx.api.system.domain.vo.UserVO>>
     * @Author: liushuai
     * @Date: 2022/10/8 21:47
     */
    @Operation(summary = "分页查询组织机构")
    @GetMapping("/listUserPage")
    public R<Page<UserVO>> listUserPage(UserQuery userQuery){
        Page<UserVO> page = userService.listUserPage(userQuery);
        return  R.success(page);
    }


    /**
     * 新增用户
     * @param userVO
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/6/30 19:17
     */
    @Operation(summary = "新增用户")
    @PostMapping("/insertUser")
    public R insertUser( @RequestBody @Validated UserVO userVO){
        userService.insertUser(userVO);
        return R.successMsg("保存成功");
    }

    /**
     * 修改用户
     * @param userVO
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/7/4 20:52
     */
    @Operation(summary = "修改用户")
    @PostMapping("/updateUser")
    public R updateUser( @RequestBody @Validated(Update.class) UserVO userVO){
        userService.updateUser(userVO);
        return R.successMsg("修改成功");
    }

    /**
     * 修改密码
     * @param updatePasswordVO
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/7/15 15:20
     */
    @Operation(summary = "修改密码")
    @PostMapping("/updatePassword")
    public R updatePassword( @RequestBody  @Validated UpdatePasswordVO updatePasswordVO){
        userService.updatePassword(updatePasswordVO);
        return R.successMsg("修改密码成功");
    }


    /**
     * 根据用户Id删除用户
     * @param userId
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/7/6 17:03
     */
    @Operation(summary = "根据用户ID删除用户")
    @GetMapping("/deleteUserByUserId")
    public R deleteUserByUserId(@RequestParam Long userId){
        userService.deleteUserByUserId(userId);
        return R.successMsg("删除成功");
    }

    /**
     * 根据用户登录名获取用户信息
     * @param username
     * @return: com.bjx.common.core.domain.R<com.bjx.api.system.domain.vo.UserVO>
     * @Author: liushuai
     * @Date: 2022/6/30 19:17
     */
    @Operation(summary = "根据用户登录名获取用户信息")
    @GetMapping("/selectUserInfoByUsername")
    public R<UserVO> selectUserInfoByUsername(@RequestParam String username){
        UserVO userVO = userService.selectUserInfoByUsername(username);
        return R.success(userVO);
    }

    public static void main(String[] args) {
        // 定义私钥和公钥
        String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAM6HGPnOSiHgCbjb62W+LkSaMF4ZmxBgj3YN2RWTx6ML7LwBMMdviheQuWZKvfw9RT5whWrCrArVqCJzCysfizf+F4YFYRbRBmzMHfxVTN9LrpGJS/lTx6QYjGyGzI/6HFmJhy+DxpRbfwzkKdxslYyVQX173Fwu9PUArsMQzbuxAgMBAAECgYAGgEwx+ONug43qyslzL3RnZm8lVysBbfi2H6N4wWkkYci29lahwIoZSc38QVgPq6IVYBdjD/O4d5dOVk7AscKBFzzNvjvCgAyqruKy//An5tiUTkA+vuo8CplorMk2/Wp6qjqKw9LonE1HpZeKxffmaaBOwhXNvTeaNGaCvfVLHQJBAOq6y+qigFpzeDVnkLFzeW9Eotd70IIEe+zvPNXSSCX6+uy4jJcY7Vo6xHO/lU0LTschiY+3DxMWSpG9RWp/fGcCQQDhPhQwpW4oQHfPAKQlgVAlEk4kR+hlbueKHZYqmcP3MJ9solYw2EkqeoRkG09/gMNi0hmY71fm9u9tfVLncvgnAkA5nTvsDZLfoLQCt7bmN2e5DenCZluZbixUzavq98BJwj7Q18N6Wt33ZbFOj9m/nWTqr6qqIc6Las9G0mQ1bbFFAkEAgbtSv64Y44A2QXkaEiwH8EI51bXBCeM14xwlaMYF0/Mib9yUji+U/K1yYYJFaKfOYbWnXqnEVFg4ETUUshBLEwJBAK6sdKsaKORBOgP7sNErng7u9Prs7QAebyv8Uj6sg9HBIE1Cn6s3x3O1RW1KpAzOaqdDQ7jkyrEMMcIcYepg4rU=";
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOhxj5zkoh4Am42+tlvi5EmjBeGZsQYI92DdkVk8ejC+y8ATDHb4oXkLlmSr38PUU+cIVqwqwK1agicwsrH4s3/heGBWEW0QZszB38VUzfS66RiUv5U8ekGIxshsyP+hxZiYcvg8aUW38M5CncbJWMlUF9e9xcLvT1AK7DEM27sQIDAQAB";

// 文本
        String text = "admin123";

// 使用公钥加密
        String ciphertext = SaSecureUtil.rsaEncryptByPublic(publicKey, text);
        System.out.println("公钥加密后：" + ciphertext);

// 使用私钥解密
        String text2 = SaSecureUtil.rsaDecryptByPrivate(privateKey, ciphertext);
        System.out.println("私钥解密后：" + text2);

    }

    /**
     * 根据用户ID获取用户信息
     * @param userId
     * @return: com.bjx.common.core.domain.R<com.bjx.api.system.domain.vo.UserVO>
     * @Author: liushuai
     * @Date: 2022/6/30 19:18
     */
    @Operation(summary = "根据用户ID获取用户信息")
    @GetMapping("/selectUserInfoByUserId")
    public R<UserVO> selectUserInfoByUserId(@RequestParam Long userId){
        UserVO userVO = userService.selectUserInfoByUserId(userId);
        return R.success(userVO);
    }

    /**
     * 根据组织机构ID查询用户列表
     * @param orgId
     * @return: com.bjx.common.core.domain.R<java.util.List<com.bjx.api.system.domain.vo.UserVO>>
     * @Author: liushuai
     * @Date: 2022/7/6 17:10
     */
    @Operation(summary = "根据组织机构ID查询用户列表")
    @GetMapping("/listUserInfoByOrgId")
    public R<List<UserVO>> listUserInfoByOrgId(@RequestParam Long orgId){
        List<UserVO> userVOS = userService.listUserInfoByOrgId(orgId);
        return R.success(userVOS);
    }


    /**
     * 保存用户职位关联
     * @param userVO
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/10/16
     */
    @Operation(summary = "保存用户职位关联")
    @PostMapping("/saveUserPost")
    public R saveUserPost(@RequestBody UserVO userVO){
        userService.saveUserPost(userVO);
        return  R.successMsg("设置职位成功");
    }

    /**
     * 根据用户id查询所关联的职位
     * @param userId
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/10/16
     */
    @Operation(summary = "根据用户id查询所关联的职位")
    @GetMapping("/listUserPostByUserId")
    public R listUserPostByUserId(@RequestParam Long userId){
        List<UserPostVO> vos = userService.listUserPost(userId);
        return R.success(vos);
    }


    /**
     * 保存用户角色关联
     * @param userVO
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/10/17 17:05
     */
    @Operation(summary = "保存用户角色关联")
    @PostMapping("/saveUserRole")
    public R saveUserRole(@RequestBody UserVO userVO){
        userService.saveUserRole(userVO);
        return  R.successMsg("设置角色成功");
    }

    /**
     * 根据用户id查询所关联的角色
     * @param userId
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/10/16
     */
    @Operation(summary = "根据用户id查询所关联的角色")
    @GetMapping("/listUserRoleByUserId")
    public R listUserRoleByUserId(@RequestParam Long userId){
        List<UserRoleVO> vos = userService.listUserRoleByUserId(userId);
        return R.success(vos);
    }


    /**
     * 根据组织机构查询当前组织机构及以下人员树
     * @param orgId
     * @return: com.bjx.common.core.domain.R
     * @Author: liushuai
     * @Date: 2022/10/20 16:13
     */
    @Operation(summary = "根据组织机构查询当前组织机构及以下人员树")
    @GetMapping("/listUserTreeByOrgId")
    public R listUserTreeByOrgId(@RequestParam Long orgId){
        List<Tree<String>> tree = userService.listUserTreeByOrgId(orgId);
        return R.success(tree);
    }


    /**
     * 根据用户id列表查询用户信息列表
     * @param userQuery
     * @return: com.bjx.common.core.domain.R<java.util.List<com.bjx.api.system.domain.vo.UserVO>>
     * @Author: liushuai
     * @Date: 2022/11/18 20:10
     */
    @PostMapping("listUserVOByIds")
    @Operation(summary = "根据用户Id列表查询用户信息")
    public R<List<UserVO>> listUserVOByIds(@RequestBody UserQuery userQuery){
       List<UserVO> list = userService.listUserVOByIds(userQuery);
       return  R.success(list);
    }

    /**
     * 根据组织机构和职位列表查询用户信息列表
     * @param userQuery
     * @return: com.bjx.common.core.domain.R<java.util.List<com.bjx.api.system.domain.vo.UserVO>>
     * @Author: liushuai
     * @Date: 2022/12/1 11:38
     */
    @PostMapping("listUserVOByOrgAndPosts")
    public R<List<UserVO>> listUserVOByOrgAndPosts(@RequestBody UserQuery userQuery){
        List<UserVO> list = userService.listUserVOByOrgAndPosts(userQuery);
        return  R.success(list);
    }

    /**
     * 根据公司名称和职位列表查询用户列表
     * @param userQuery
     * @return: com.bjx.common.core.domain.R<java.util.List<com.bjx.api.system.domain.vo.UserVO>>
     * @Author: liushuai
     * @Date: 2022/12/1 11:38
     */
    @PostMapping("listUserVOByComAndPosts")
    public R<List<UserVO>> listUserVOByComAndPosts(@RequestBody UserQuery userQuery){
        List<UserVO> list = userService.listUserVOByComAndPosts(userQuery);
        return  R.success(list);
    }

    /**
     * 根据角色id列表查询用户信息
     * @param userQuery
     * @return: com.bjx.common.core.domain.R<java.util.List<com.bjx.api.system.domain.vo.UserVO>>
     * @Author: liushuai
     * @Date: 2022/12/2 15:37
     */
    @PostMapping("listUserVOByRoleIds")
    public R<List<UserVO>> listUserVOByRoleIds(@RequestBody UserQuery userQuery){
        List<UserVO> list = userService.listUserVOByRoleIds(userQuery);
        return  R.success(list);
    }

    /**
     * 根据职位id列表查询用户信息
     * @param userQuery
     * @return: com.bjx.common.core.domain.R<java.util.List<com.bjx.api.system.domain.vo.UserVO>>
     * @Author: liushuai
     * @Date: 2022/12/2 15:59
     */
    @PostMapping("listUserVOByPostIds")
    public R<List<UserVO>> listUserVOByPostIds(@RequestBody UserQuery userQuery){
        List<UserVO> list = userService.listUserVOByPostIds(userQuery);
        return  R.success(list);
    }

    /**
     * 根据组织机构id和角色id列表查询用户列表
     * @param userQuery
     * @return: com.bjx.common.core.domain.R<java.util.List<com.bjx.api.system.domain.vo.UserVO>>
     * @Author: liushuai
     * @Date: 2022/12/5 17:34
     */
    @PostMapping("listUserVOByOrgAndRoleIds")
    public R<List<UserVO>> listUserVOByOrgAndRoleIds(@RequestBody UserQuery userQuery){
        List<UserVO> list = userService.listUserVOByOrgAndRoleIds(userQuery);
        return  R.success(list);
    }

    /**
     * 根据公司id和角色id列表
     * @param userQuery
     * @return: com.bjx.common.core.domain.R<java.util.List<com.bjx.api.system.domain.vo.UserVO>>
     * @Author: liushuai
     * @Date: 2022/12/5 19:29
     */
    @PostMapping("listUserVOByComAndRoleIds")
    public R<List<UserVO>> listUserVOByComAndRoleIds(@RequestBody UserQuery userQuery){
        List<UserVO> list = userService.listUserVOByComAndRoleIds(userQuery);
        return  R.success(list);
    }



}
